package day19;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author: 王丹
 * @create: 2021-07-07 15:46
 **/
public class Demo9 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        FutureTask<Integer> futureTask = new FutureTask<Integer>(() -> {
//            System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + ",start!");
//            TimeUnit.SECONDS.sleep(5);
//            System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + ",end!");
//            return 10;
//        });
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + ",start!");
                TimeUnit.SECONDS.sleep(5);
                System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + ",end!");
                return 10;
            }
        });
        System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName());
        new Thread(futureTask).start();
        /**
         * 1625647531466,main
         * 1625647531466,main
         * 1625647531466,Thread-0,start!
         * 1625647536468,Thread-0,end!
         * 1625647531466,main,结果:10
         */
        //futureTask.run();
        /**
         * 1625647421957,main
         * 1625647421958,main,start!
         * 1625647426959,main,end!
         * 1625647426959,main
         * 1625647426959,main,结果:10
         */
        System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName());
        System.out.println(System.currentTimeMillis() + "," + Thread.currentThread().getName() + ",结果:" + futureTask.get());
    }
}
